package cn.hsa.fqz.task.controller;

import cn.hsa.fqz.common.enums.FileType;
import cn.hsa.fqz.handler.base.BaseController;
import cn.hsa.fqz.task.entity.TTaskClueSubject;
import cn.hsa.fqz.task.enums.UserCategoryEnums;
import cn.hsa.fqz.task.service.TClueTaskService;
import cn.hsa.fqz.task.service.TSysFileAccessoryService;
import cn.hsa.fqz.task.service.dto.LoginUserDTO;
import cn.hsa.fqz.task.service.dto.TaskClueQueryDTO;
import cn.hsa.fqz.util.UserRegionUtils;
import cn.hsa.hsaf.core.framework.context.HsafContextHolder;
import cn.hsa.hsaf.core.framework.util.CurrentUser;
import cn.hsa.hsaf.core.framework.util.PageResult;
import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 丁志敏
 * @version v1.0
 * @apiNote 功能描述:()
 * @date create in 2022/9/21 17:15
 */
@Slf4j
@Api(value = "任务管理", tags = "任务管理")
@RestController
@RequestMapping("/apply")
public class TClueController extends BaseController {

    @Autowired
    private TClueTaskService clueTaskService;

    @Autowired
    private TSysFileAccessoryService sysFileAccessoryService;


    @ApiOperation(value = "任务申请", notes = "任务申请")
    @PostMapping("/putin")
    public WrapperResponse<Boolean> putin(@RequestBody TTaskClueSubject subject) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        log.info("当前用户信息: "+ JSON.toJSONString(currentUser));
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return clueTaskService.addPutin(loginUser, subject);
    }

    @ApiOperation(value = "获取任务申请", notes = "获取任务申请")
    @GetMapping("/subject")
    public WrapperResponse<TTaskClueSubject> getById(@RequestParam("subjectId")Integer subjectId){
        return clueTaskService.getById(subjectId);
    }

    @ApiOperation(value = "任务申请列表", notes = "任务申请列表")
    @PostMapping("/list")
    public WrapperResponse<PageResult<TTaskClueSubject>> applyList(@RequestBody TaskClueQueryDTO query) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        log.info("当前用户信息: "+ JSON.toJSONString(currentUser));
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        query.getCondition().setUname(loginUser.getUid());
        return clueTaskService.getPageByCondition(query);
    }

/*    public static void main(String[] args) {
        String str = "5";
        Map mmp = new HashMap();
        mmp.put("1", str);
        int x = (int) mmp.get("1");
        int b = x+3;
        System.out.println(b);
    }*/

    @ApiOperation(value = "审核任务", notes = "审核任务")
    @GetMapping("/audit")
    public WrapperResponse<Boolean> audit(@RequestParam("subjectId") Integer subjectId,
                                     @RequestParam("isAudit") Integer isAudit,
                                     @RequestParam(value = "rejectReason",required = false) String rejectReason
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        log.info("当前用户信息: "+ JSON.toJSONString(currentUser));
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return clueTaskService.addAudit(loginUser, subjectId,isAudit,rejectReason);
    }

    @ApiOperation(value = "编辑申请任务", notes = "编辑申请任务")
    @PostMapping("/edit")
    public WrapperResponse<Boolean> edit(@RequestBody TTaskClueSubject subject
    ) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        log.info("当前用户信息: "+ JSON.toJSONString(currentUser));
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        return clueTaskService.edit(loginUser, subject);
    }

    @ApiOperation(value = "任务审核列表", notes = "任务审核列表")
    @PostMapping("/audit/list")
    public WrapperResponse<PageResult<TTaskClueSubject>> auditList(
                                                   @RequestBody TaskClueQueryDTO query) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        log.info("当前用户信息: "+ JSON.toJSONString(currentUser));
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(loginUser.getRegion());
        query.getCondition().setBelongTo(enums.name());
        return clueTaskService.getPageAuditByCondition(query);
    }

    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/file/upload")
    public WrapperResponse<String> uploadFile(@RequestParam MultipartFile file) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO dto = super.getLoginUser(currentUser);
        return WrapperResponse.success(sysFileAccessoryService.upload("0", FileType.CE,dto, file));
    }

}
